<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetData } from '@/api/modules/account'
import { GetStopOption } from '@/api/modules/shop'
// import PageNumber from '@/store/modules/PageNumberCache'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
// const PageNumberCache = PageNumber()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('account').currentPage

const findFrom = reactive({
  ...(cacheData.GetCacheData('account').fromData),
})
const dataList: any = reactive({
  list: [],
  shopOption: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.RecShopId !== '~' && ({ RecShopId: findFrom.RecShopId })),
    ...(findFrom.RecShopName && ({ RecShopName: findFrom.RecShopName })),
    ...(findFrom.Id && ({ Id: findFrom.Id })),
    ...(findFrom.UserId && ({ UserId: findFrom.UserId })),
    ...(findFrom.RealName && ({ RealName: findFrom.RealName })),
    ...(findFrom.UserMobile && ({ UserMobile: findFrom.UserMobile })),
    ...(findFrom.RoleId !== '~' && ({ RoleId: findFrom.RoleId })),
    ...(findFrom.ParentId && ({ ParentId: findFrom.ParentId })),
    ...(findFrom.ParentUserId && ({ ParentUserId: findFrom.ParentUserId })),
    ...(findFrom.ParentRealName && ({ ParentRealName: findFrom.ParentRealName })),
    ...(findFrom.ParentUserMobile && ({ ParentUserMobile: findFrom.ParentUserMobile })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
    }
  })
  loading.value = false
}

function Inquire() {
  GetDataList(1, cacheData.GetCacheData('account').currentSize)
  cacheData.SetCacheData('account', PageNum, cacheData.GetCacheData('account').currentSize, findFrom.value)
}
// 业绩明细跳转
// function AddData() {
//   router.push({
//     name: 'performanceList',
//   })
// }
// 编辑
function EditData(id: number) {
  router.push({
    name: 'accountDetail',
    query: {
      id,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('account', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('account').currentSize)
  cacheData.SetCacheData('account', val, cacheData.GetCacheData('account').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('account').currentPage || 1
})

onMounted(async () => {
  try {
    await GetStopOption().then((res) => {
      dataList.shopOption = res.data
    })

    await GetDataList(cacheData.GetCacheData('account').currentPage, cacheData.GetCacheData('account').currentSize)
  }
  catch (error) {
    console.error('Error fetching data:', error)
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="店铺">
                <el-select v-model="findFrom.RecShopId" placeholder="请选择店铺" filterable>
                  <el-option label="全部" value="~" />
                  <el-option v-for="item in dataList.shopOption" :key="item.id" :label="item.shopName" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="店铺名称">
                <el-input v-model="findFrom.RecShopName" placeholder="请输入店铺名称" clearable />
              </el-form-item>
              <el-form-item label="账户ID">
                <el-input v-model="findFrom.Id" placeholder="请输入账户ID" clearable />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable />
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="findFrom.RealName" placeholder="请输入姓名" clearable />
              </el-form-item>
              <el-form-item label="手机号">
                <el-input v-model="findFrom.UserMobile" placeholder="请输入手机号" clearable />
              </el-form-item>
              <el-form-item label="角色">
                <el-select v-model="findFrom.RoleId" placeholder="请选择角色">
                  <el-option label="全部" value="~" />
                  <el-option label="店员" :value="1" />
                  <el-option label="店长" :value="2" />
                </el-select>
              </el-form-item>
              <el-form-item label="上级账户ID">
                <el-input v-model="findFrom.ParentId" placeholder="请输入上级账户ID" clearable />
              </el-form-item>
              <el-form-item label="上级用户ID">
                <el-input v-model="findFrom.ParentUserId" placeholder="请输入上级用户ID" clearable />
              </el-form-item>
              <el-form-item label="上级姓名">
                <el-input v-model="findFrom.ParentRealName" placeholder="请输入上级姓名" clearable />
              </el-form-item>
              <el-form-item label="上级手机号">
                <el-input v-model="findFrom.ParentUserMobile" placeholder="请输入上级手机号" clearable />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <!-- <div class="mb-5 text-right">
        <el-button @click="AddData">
          查看业绩明细
        </el-button>
      </div> -->
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="shop.id" label="店铺ID" width="120" />
        <el-table-column align="center" prop="shop.shopName" label="店铺名称" width="300" />
        <el-table-column align="center" prop="shop.shopPic" label="店铺图片" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.shop?.shopPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="subscriber.userId" label="用户ID" width="120" />
        <el-table-column align="center" prop="subscriber" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriber?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriber?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="300" prop="subscriber.scOpenId" label="用户OpenID" />
        <el-table-column align="center" width="300" prop="subscriber.unionId" label="用户unionId" />
        <el-table-column align="center" prop="parentId" label="上级账户ID" width="260" />
        <el-table-column align="center" prop="parentRealName" label="上级姓名" width="120" />
        <el-table-column align="center" prop="parentUserMobile" label="上级手机号" width="150" />
        <el-table-column align="center" prop="roleId" label="角色" width="100">
          <template #default="scope">
            <el-tag>
              {{ scope.row.roleId === 1 ? '店员' : '店长' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="realName" label="姓名" width="300" />
        <el-table-column align="center" prop="userStatus" label="账户状态" width="100">
          <template #default="scope">
            <el-tag>
              {{ scope.row.userStatus === 1 ? '启用' : '冻结' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="percents" label="店长分润百分比" width="150" />
        <el-table-column header-align="center" prop="desp" label="描述" width="300" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="200" />
        <el-table-column align="center" prop="bindTime" label="绑定身份时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="200" />
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('account').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;
  margin-right: 25px;

  div {
    text-align: left;
  }
}
</style>
